package guoanmaker.operator.business.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import guoanmaker.operator.business.model.OperatorHelp;
import guoanmaker.operator.business.modelnobind.OperatorHelpMove;
import guoanmaker.operator.business.service.OperatorHelpServiceInterface;
import guoanmaker.operator.helper.common.AjaxJson;
import guoanmaker.operator.helper.common.abstractobj.ApiController;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(ApiController.OPERATOR_HELP)
public class OperatorHelpController {
	@Autowired
	 private  AjaxJson ajaxJson;
	@Autowired
	private OperatorHelpServiceInterface operatorHelpServiceInterface;
	
	  @ApiOperation(value = "保存帮助中心", notes = "保存帮助中心，包括新增与修改 ")
	    @PostMapping(value = "saveHelp")
	    public AjaxJson saveHelp(@RequestBody OperatorHelp operatorHelp) {
		  operatorHelp.setCreateDate(new Date());
		  try {
			  OperatorHelp oh=operatorHelpServiceInterface.saveOperatorHelp(operatorHelp);
			  if(oh!=null){
				  ajaxJson.setSuccess(true).setStatus(200).setData(oh);
			  }else{
				  ajaxJson.setSuccess(true).setStatus(230).setData("标题已存在");
			  }
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(true).setStatus(230).setData("保存失败");
		}
	        return ajaxJson;
	    }
	  
	  /**
	   * 启用1 禁用2
	  * @Title: updateHelpBystart 
	  * @Description: TODO(这里用一句话描述这个方法的作用) 
	  * @param id
	  * @return    设定文件 
	  * @return AjaxJson    返回类型 
	  * @throws 
	   */
	  @ApiOperation(value = "启用和禁用操作", notes = "启用和禁用操作 ")
	    @PostMapping(value = "updateHelpBystatus")
	    public AjaxJson updateHelpBystart(HttpServletRequest request, HttpServletResponse response,String id,Integer status,String userid) {
		  
		  OperatorHelp ohelp= operatorHelpServiceInterface.getHelpById(id);
		  
		  ohelp.setStatus(status);
		  ohelp.getOperatorUser().setId(userid);
		  OperatorHelp oh=operatorHelpServiceInterface.saveOperatorHelp(ohelp);
		  if(null!=oh){
	        ajaxJson.setSuccess(true).setStatus(200).setData(oh);
	        }else{
	        	ajaxJson.setSuccess(true).setStatus(250).setMsg("启用失败");
	        }
	        return ajaxJson;
	    }
	  
	  
	  @ApiOperation(value = "分页查询保存帮助中心", notes = "分页查询保存帮助中心")
	  @PostMapping(value = "getAllHelpByPage")
	  
	    public AjaxJson getAllHelpByPage(@RequestParam(value = "page", defaultValue = "0") Integer page,
	                                           @RequestParam(value = "size", defaultValue = "10") Integer size) {
		  Map<String,Object> map= operatorHelpServiceInterface.getAllHelpByPage(page, size);
		  int a=(int) map.get("count");
		  if(a>0){
	        ajaxJson.setSuccess(true).setStatus(200).setData(map);
	        }else{
	        	ajaxJson.setSuccess(false).setStatus(250).setMsg("没有查询出数据");
	        }
		  return  ajaxJson;
	    }
	  @ApiOperation(value = "查询保存帮助中心", notes = "查询保存帮助中心")
	  @PostMapping(value = "getAllHelp")
	  public AjaxJson getAllHelp() {
		  List<OperatorHelpMove> list= operatorHelpServiceInterface.getAllHelp();
		  if(list!=null&&list.size()>0){
			  ajaxJson.setSuccess(true).setStatus(200).setData(list);
		  }else{
			  ajaxJson.setSuccess(false).setStatus(250).setMsg("没有查询出数据");
		  }
		  return  ajaxJson;
	  }
	  
	  
	  @ApiOperation(value = "删除保存帮助中心", notes = "根据Id删除保存帮助中心")
	  @PostMapping(value = "deleteHelp")
	    
	    public AjaxJson deleteHelpById(String id) {
		  try{
		  operatorHelpServiceInterface.deleteHelpById(id);
		  ajaxJson.setSuccess(true).setStatus(200).setData("数据删除成功!");
		  }catch(Exception e){
			  e.printStackTrace();
			  ajaxJson.setSuccess(false).setStatus(250);
		  }
	        return ajaxJson;
	    }
	  
	  @ApiOperation(value = "根据Id查询a帮助中心信息", notes = "根据Id查询a帮助中心信息")
	  @PostMapping(value = "getHelpById")

/*
	    public AjaxJson getHelpById(@RequestParam(value = "id") String id) {
	        return ajaxJson.setSuccess(true).setStatus(200).setData(operatorHelpServiceInterface.getHelpById(id));
	    }
*/
	    public AjaxJson getHelpById(String id) {
		  try {
			  ajaxJson.setSuccess(true).setStatus(200).setData(operatorHelpServiceInterface.getHelpById(id));
		} catch (Exception e) {
			// TODO: handle exception
			ajaxJson.setSuccess(true).setStatus(250).setData("查询失败");
		}
	        return ajaxJson;
	    
	  }

	  
	  @ApiOperation(value = "APP分页查询保存帮助中心", notes = "APP分页查询保存帮助中心")
	  @PostMapping(value = "getAPPAllHelpByPage")
	  public AjaxJson getAPPAllHelpByPage(@RequestParam(value = "page", defaultValue = "0") Integer page,
	                                           @RequestParam(value = "size", defaultValue = "10") Integer size) {
		  Page<OperatorHelp> helps= operatorHelpServiceInterface.getAllHelpBystatusPage(page, size);
		  if(helps.getContent().size()!=0){
	        ajaxJson.setSuccess(true).setStatus(200).setData(helps);
	        }else{
	        	ajaxJson.setSuccess(false).setStatus(250).setMsg("没有查询出数据").setData(null);
	        }
		  return  ajaxJson;
	    }



	  /*  public AjaxJson getHelpById(String id) {
		  try {
			  ajaxJson.setSuccess(true).setStatus(200).setData(operatorHelpServiceInterface.getHelpById(id));
		} catch (Exception e) {
			// TODO: handle exception
			ajaxJson.setSuccess(true).setStatus(250).setData("查询失败");
		}
	        return ajaxJson;
	    
	  }*/


}
